package com.gsx.service.Impl;

import com.gsx.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MyUserDetailServiceImpl implements UserDetailsService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        if(s==null){
            throw new UsernameNotFoundException("用户名不存在");
        }

        String password=userMapper.getUser(s);
        //权限
        String auth=userMapper.getAuth(s);
        List<GrantedAuthority> authorities= AuthorityUtils.commaSeparatedStringToAuthorityList(auth);
        return new User(s, new BCryptPasswordEncoder().encode(password),authorities);
    }
}
